package cn.stylefeng.guns.modular.hp.controller;

import cn.hutool.core.date.DateUtil;
import cn.stylefeng.guns.config.properties.HpProperties;
import cn.stylefeng.guns.core.common.annotion.BussinessLog;
import cn.stylefeng.guns.core.common.exception.BizExceptionEnum;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.hp.domain.ArticleInfoDO;
import cn.stylefeng.guns.modular.hp.model.ArticleInfo;
import cn.stylefeng.guns.modular.hp.model.FileDownloadInfo;
import cn.stylefeng.guns.modular.hp.model.PageContentInfo;
import cn.stylefeng.guns.modular.hp.service.IArticleInfoService;
import cn.stylefeng.guns.modular.hp.service.IFileDownloadInfoService;
import cn.stylefeng.guns.modular.hp.service.IPageContentInfoService;
import cn.stylefeng.guns.modular.hp.warpper.ArticleInfoWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 医院公告控制器
 *
 * @author xzqiang
 * @date 2018-12-09
 */
@Controller
@RequestMapping("/hp/article")
public class ArticleInfoController extends BaseController {

    private String PREFIX = "/hp/";
    
    @Autowired
    private HpProperties hpProperties;

    @Autowired
    private IArticleInfoService articleInfoService;
    
    @Autowired
    private IPageContentInfoService pageContentInfoService;

    @Autowired
    private IFileDownloadInfoService fileDownloadInfoService;

    /**
     * 跳转到通知列表首页
     */
    @RequestMapping("/toindex/{modularType}")
    public String index(@PathVariable String modularType) {
    	String htmlPath = "";
    	switch (modularType) {
		case "notice":
			htmlPath = "/notice/noticeInfo.html";
			break;
		case "news":
			htmlPath = "/news/news.html";
			break;
		case "department":
			htmlPath = "/department/department.html";
			break;
		case "srt":
			htmlPath = "/srt/srtInfo.html";
			break;
		case "he":
			htmlPath = "/he/heInfo.html";
			break;
		case "kcm":
			htmlPath = "/kcm/kcmInfo.html";
			break;
		case "ng":
			htmlPath = "/ng/ngInfo.html";
			break;
		case "pbg":
			htmlPath = "/pbg/pbgInfo.html";
			break;
		case "mip":
			htmlPath = "/mip/mipInfo.html";
			break;
		case "pa":
			htmlPath = "/pa/paInfo.html";
			break;
		case "pr":
			htmlPath = "/pr/prInfo.html";
			break;
		default:
			break;
		}
    	
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到添加通知
     */
    @RequestMapping("/info_add/{modularType}")
    public String infoAdd(@PathVariable String modularType) {
    	String htmlPath = "";
    	switch (modularType) {
		case "notice":
			htmlPath = "/notice/noticeInfo_add.html";
			break;
		case "news":
			htmlPath = "/news/news_add.html";
			break;
		case "department":
			htmlPath = "/department/department_add.html";
			break;
		case "srt":
			htmlPath = "/srt/srtInfo_add.html";
			break;
		case "he":
			htmlPath = "/he/heInfo_add.html";
			break;
		case "kcm":
			htmlPath = "/kcm/kcmInfo_add.html";
			break;
		case "ng":
			htmlPath = "/ng/ngInfo_add.html";
			break;
		case "pbg":
			htmlPath = "/pbg/pbgInfo_add.html";
			break;
		case "mip":
			htmlPath = "/mip/mipInfo_add.html";
			break;
		case "pa":
			htmlPath = "/pa/paInfo_add.html";
			break;
		case "pr":
			htmlPath = "/pr/prInfo_add.html";
			break;
		default:
			break;
		}
    	
        return PREFIX + htmlPath;
    }

    /**
     * 跳转到修改通知
     */
    @RequestMapping("/info_update/{modularType}/{infoId}")
    public String infoUpdate(@PathVariable String modularType, @PathVariable String infoId, Model model) {
        ArticleInfo info = this.articleInfoService.selectById(infoId);
        
        PageContentInfo pageContent = this.pageContentInfoService.selectById(info.getPageContentId());
        
        ArticleInfoDO articleInfoDO = new ArticleInfoDO();
        articleInfoDO.setId(info.getId());
        articleInfoDO.setModular(info.getModular());
        articleInfoDO.setTitle(info.getTitle());
        articleInfoDO.setContent(pageContent.getContent());
        articleInfoDO.setPri(info.getPri());
		articleInfoDO.setCreatedTime(DateUtil.format(info.getCreatedTime(), "yyyy-MM-dd HH:mm:ss"));
        
        model.addAttribute("info", articleInfoDO);
        LogObjectHolder.me().set(articleInfoDO);
        
        String htmlPath = "";
    	switch (modularType) {
		case "notice":
			htmlPath = "/notice/noticeInfo_edit.html";
			break;
		case "news":
			htmlPath = "/news/news_edit.html";
			break;
		case "department":
			htmlPath = "/department/department_edit.html";
			break;
		case "srt":
			htmlPath = "/srt/srtInfo_edit.html";
			break;
		case "he":
			htmlPath = "/he/heInfo_edit.html";
			break;
		case "kcm":
			htmlPath = "/kcm/kcmInfo_edit.html";
			break;
		case "ng":
			htmlPath = "/ng/ngInfo_edit.html";
			break;
		case "pbg":
			htmlPath = "/pbg/pbgInfo_edit.html";
			break;
		case "mip":
			htmlPath = "/mip/mipInfo_edit.html";
			break;
		case "pa":
			htmlPath = "/pa/paInfo_edit.html";
			break;
		case "pr":
			htmlPath = "/pr/prInfo_edit.html";
			break;
		default:
			break;
		}
    	
        return PREFIX + htmlPath;
    }

    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list/{modularType}")
    @ResponseBody
    public Object list(@PathVariable String modularType, String condition) {
        List<Map<String, Object>> list = this.articleInfoService.list(modularType, condition);
        return super.warpObject(new ArticleInfoWrapper(list));
    }

    /**
     * 新增通知
     */
    @RequestMapping(value = "/add/{modularType}")
    @ResponseBody
    @BussinessLog(value = "新增通知", key = "title")
    public Object add(@PathVariable String modularType, ArticleInfoDO articleInfoDO) {
        if (ToolUtil.isOneEmpty(articleInfoDO, articleInfoDO.getTitle(), articleInfoDO.getContent())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        //新增页面内容
        PageContentInfo pageContent = new PageContentInfo();
        pageContent.setContent(articleInfoDO.getContent());
        pageContent.setCreater(ShiroKit.getUser().getId());
        pageContent.setCreatedTime(articleInfoDO.getCreatedTime() == null ? new Date() : DateUtil.parse(articleInfoDO.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
        pageContent.insert();
        
        //新增公告内容
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setModular(modularType);
        articleInfo.setTitle(articleInfoDO.getTitle());
        articleInfo.setLinkUrl(pageContent.getId());
        articleInfo.setPageContentId(pageContent.getId());
        articleInfo.setPri(articleInfoDO.getPri());
        articleInfo.setCreater(ShiroKit.getUser().getId());
        articleInfo.setCreatedTime(articleInfoDO.getCreatedTime() == null ? new Date() : DateUtil.parse(articleInfoDO.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
        articleInfo.insert();

        //更新文章附件信息
		if(articleInfoDO.getAttachList() != null && articleInfoDO.getAttachList().length() > 0){
			System.out.println("收到附件信息");
			String[] fileIds = articleInfoDO.getAttachList().split(",");
			//遍历ID，把附件temp标识设为0，加入文章ID 关联
			for(String fileId : fileIds){
				FileDownloadInfo fileInfo = fileDownloadInfoService.selectById(fileId);
				//把附件temp标识设为0，加入文章ID 关联
				if(fileInfo != null){
					fileInfo.setTemp(FileDownloadInfo.TEMP_NO);
					fileInfo.setPageContentInfoId(articleInfo.getId());
					fileInfo.updateById();
				}
			}
		}

        return SUCCESS_TIP;
    }

    /**
     * 删除通知
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @BussinessLog(value = "删除通知", key = "infoId")
    public Object delete(@RequestParam String infoId) {

        this.articleInfoService.deleteById(infoId);

        return SUCCESS_TIP;
    }

    /**
     * 修改通知
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @BussinessLog(value = "修改通知")
    public Object update(ArticleInfoDO articleInfoDO) {
        if (ToolUtil.isOneEmpty(articleInfoDO, articleInfoDO.getId(), articleInfoDO.getTitle(), articleInfoDO.getContent())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        
        ArticleInfo oldArticleInfo = this.articleInfoService.selectById(articleInfoDO.getId());
        PageContentInfo oldPageContent = this.pageContentInfoService.selectById(oldArticleInfo.getPageContentId());
        
        
        oldPageContent.setContent(articleInfoDO.getContent());
		oldPageContent.setCreatedTime(articleInfoDO.getCreatedTime() == null ? new Date() : DateUtil.parse(articleInfoDO.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
    	oldPageContent.updateById();
        
    	oldArticleInfo.setTitle(articleInfoDO.getTitle());
    	oldArticleInfo.setPageContentId(oldPageContent.getId());
    	oldArticleInfo.setLinkUrl(oldPageContent.getId());
    	oldArticleInfo.setPri(articleInfoDO.getPri());
		oldArticleInfo.setCreatedTime(articleInfoDO.getCreatedTime() == null ? new Date() : DateUtil.parse(articleInfoDO.getCreatedTime(),"yyyy-MM-dd HH:mm:ss"));
    	oldArticleInfo.updateById();
        return SUCCESS_TIP;
    }

}
